<!-- 新机直购订单管理 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="订单列表" name="one">
        <SearchTable :deliveryTypeList="deliveryTypeList" :selectPlatformType="selectPlatformType" :selectWhether="selectWhether" :orderStatusList="orderStatusList" :logisticsList="logisticsList" ref="searchTableRef" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="订单详情" name="two">
        <FormTab :id="selectId" v-model:formStatus="formStatus" @handleInitData="clearData" @handleSuccess="handleSuccess" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="directPurchaseOrderInfo">
import SearchTable from "@/components/rentMachineManage/directPurchaseOrderInfo/SearchTable.vue";
import FormTab from "@/components/rentMachineManage/directPurchaseOrderInfo/FormTab.vue";
import { useDictData } from "@/hooks/network/useDictData";
import type { IDictData } from "@/types/dict";
import type { TabsPaneContext } from "element-plus";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { IExpressOften } from "@/types/express";
import { getLogisticsInfo } from "@/utils/api/express";

let { getDictData } = useDictData();
/* 表格状态 */
const formStatus = ref<ViewMode>("none");
/* 标签页切换 */
const activeName = ref<TabsMode>("one");
const selectId = ref(0);
/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");
/* 配送方式 */
const deliveryTypeList = ref<Array<IDictData>>([]);
/* 平台类型 */
const selectPlatformType = ref<Array<IDictData>>([]);
/* 是否字段（账单是否有支付，是否有创建预授权) */
const selectWhether = ref<Array<IDictData>>([]);
/* 订单状态 */
const orderStatusList = ref<Array<IDictData>>([]);
/* 物流信息 */
const logisticsList = ref<Array<IExpressOften>>([]);

/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}

/* 处理查看编辑 */
function handleViewEdit(id: number, code: ViewModeFilter) {
  selectId.value = id;
  formStatus.value = code;
  activeName.value = "two";
}

/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  selectId.value = 0;
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}

/* 获取物流公司信息 */
function handleGetLogisticsInfo() {
  getLogisticsInfo<Array<IExpressOften>>()
    .then(res => {
      logisticsList.value = res.result;
      logisticsList.value.push({
        id: -1,
        name: "无需物流",
        shipping_code: "-1"
      });
    })
    .catch(() => {});
}

getDictData("SHIPPING_METHOD", deliveryTypeList);
getDictData("PLATFORM_TYPE", selectPlatformType);
getDictData("WHETHER", selectWhether);
getDictData("RENT_MOBILE_ORDER_STATUS", orderStatusList);

handleGetLogisticsInfo();
</script>

<style scoped></style>
